//构建导航条的状态模块
const NavModule = {
  // namespaced: true,//开启模块的命名空间，此时模块会根据注册时对应
  state: {
    title: "外卖",
    showBack: true,
    showSearch: true,
    showMy: true,
    showNav: true,
    showLogin: true,
    showTabBar: false,
  },
  mutations: {
    changeTitle (state, title) {
      state.title = title;
    },
    changeShowBack (state, showBack) {
      state.showBack = showBack;
    },
    changeShowSearch (state, showSearch) {
      state.showSearch = showSearch;
    },
    changeShowMy (state, showMy) {
      state.showMy = showMy;
    },
    changeShowNav (state, showNav) {
      state.showNav = showNav;
    },
    changeShowLogin (state, showLogin) {
      state.showLogin = showLogin;
    },
    changeShowTabBar (state, showTabBar) {
      state.showTabBar = showTabBar;
    },
  }
}

//构建状态条的全局状态模块
const TabModule = {
  // namespaced: true,
  state: {
    tabIndex: 0
  },
  mutations: {
    changeTabIndex (state, index) {
      state.tabIndex = index;
    },
  }
}
//构建购物车的全局状态模块
const CarModule = {
  state: {
    car:[]
    // JSON.parse(localStorage.getItem("car"))
  },
  mutations: {
    addToCar (state, goods) {
      // var flag = false;
      // console.log(goods);
      let res = state.car.some(item => {
        return item.shopid == goods.shopid;
      })
      if (!res) {
        state.car.push(goods);
        console.log("增加");
      };
      // goods.food.buyfood.forEach(god =>{
      //   console.log(god);
      // })
      localStorage.setItem("car", JSON.stringify(state.car));
    },
    addnew(state,goods){  
      state.car.forEach(item => {
        if (item.shopid == goods.shopId) { 
          console.log(1);
            let sd = item.food.buyfood.some(fod =>{
              return fod.foodId == goods.foodId;
            });
            if (!sd) {
              console.log("新增");
              item.food.buyfood.push(goods);
            }
        }
      })
      localStorage.setItem("car", JSON.stringify(state.car));
    },
    increase (state, good) {
      console.log(state.car);
      state.car.forEach(item => {
        console.log(good);
        if (item.shopid == good.shopId) {
          console.log(item);
          item.food.buyfood.forEach(fod => {
            if (fod.foodId == good.foodId) {
              fod.foodCount++;
              console.log("+了");
            }
          })
        }
      });
      localStorage.setItem("car", JSON.stringify(state.car));
    },
    reduce (state, good) {
      console.log(state.car);
      state.car.forEach(item => {
        if (item.shopid == good.shopId) {
          console.log(item);
          item.food.buyfood.forEach(fod => {
            if (fod.foodId == good.foodId) {
              fod.foodCount--;
              console.log("-了");
            }
          })
        }
      });
      localStorage.setItem("car", JSON.stringify(state.car));
    }
  },
  getters: {
    getAllMoney: (state) => (shopId) =>{
      var allMoney = 0;
      state.car.forEach(item => {
        if (item.shopid == shopId) {
          item.food.buyfood.forEach(fod => {
            allMoney += fod.foodCount * fod.foodPrice;
          })
        }
      });
      return allMoney;
    },
    getAllCount: (state) => (shopId) =>{
      var allCount = 0;
      state.car.forEach(item => {
        if (item.shopid == shopId) {
          item.food.buyfood.forEach(fod => {
            allCount += fod.foodCount 
          })
        }
      });
      return allCount;
    },
    // getAllMoney (state,shopId) {
    //   var allMoney = 0;
    //   state.car.forEach(item => {
    //     if (item.shopid == shopId.shopId) {
    //       item.food.buyfood.forEach(fod => {
    //         allMoney += fod.foodCount * fod.foodPrice;
    //       })
    //     }
    //   });
    //   return allMoney;
    // },
    getAllfood (state) {
      return state.car;
    }
  }
}
export default { nav: NavModule, tab: TabModule, car: CarModule };
